package com.guavestudios.fcon.commands {
	import com.guavestudios.fcon.api.IControlableConsole;
	import com.guavestudios.fcon.api.INamedCommand;
	import com.guavestudios.fcon.core.PathElement;
	import flash.utils.describeType;
	
	/**
	* ...
	* @author Default
	*/
	public class ModifyCommand implements INamedCommand{
		
		public function ModifyCommand() {
			
		}
		
		/* INTERFACE com.guavestudios.fcon.api.INamedCommand */
		
		public function execute(args:Array):void{
			var c:IControlableConsole = IControlableConsole(args.shift());
			
			var ele:PathElement = args.shift();
			var prop:String = ele.getElementId();
			var val:String = args.join(" ");
			
			if (ele == null || prop == null || val == null) {
				c.outputText(getCommandName()+": Property not found");
				return;
			}
			
			try {
				if (val.charAt(0)=="'") {
					ele.getParent().getElement()[prop] = val.substr(1, val.length - 2);
				} else if (val == "true") {
					ele.getParent().getElement()[prop] = true;
				} else if (val == "false") {
					ele.getElement()[prop] = false;
				} else if (val == "null") {
					ele.getParent().getElement()[prop] = null;
				} else {
					ele.getParent().getElement()[prop] = parseFloat(val);
				}
				c.outputText(getCommandName() + ": " + prop + "=" + val);
			} catch (ex:Error) {
				c.outputText(getCommandName()+": Property not found");
			}
			
			
			
		}
		
		public function getCommandName():String{
			return "modify";
		}
	}
}